home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 761 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.3 KB

  1. Path: solon.com!not-for-mail
  2. From: Heiner.Marxen@DrB.Insel.DE (Heiner Marxen)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: printf() format extensions - looking for beta testers...
  5. Date: 18 Apr 1996 07:36:26 -0500
  6. Organization: Dr. Brunthaler IITech GmbH, Germany
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4l5d0a$211@solutions.solon.com>
  10. References: <4kgljv$l2p@solutions.solon.com> <4klicn$ndl@solutions.solon.com> <4kr78t$kao@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4kr78t$kao@solutions.solon.com>,
  14. Casper H.S. Dik - Network Security Engineer <Casper.Dik@Holland.Sun.COM> wrote:
  15. >Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes:
  16. >> >                 format('!', bangcvt);
  17. >> > 
  18. >> >                 /* do something which can generate an error */
  19. >> >                 fprintf(stderr, "%s: %!\n", "open failed");
  20. >> >         }
  21. >
  22. >>Given the above, how about calls like (well, not a pretty one, but you get
  23. >>the idea...):
  24. >
  25. >>    fprintf(stderr, "%-*.2!", something);
  26. >
  27. >Yes, it seems that the format call back function (bangcvt) should take
  28. >a number of flags (leading 0/- found, precision, maximum width) and
  29. >a generic way to make the user defined format specifier output characters,
  30. >as it can be called from functions like "snprintf", which limit the output.
  31. >Passing a buffer to but the output in doesn't strike me as sufficient;
  32. >if "bangcvt" can have unlimited output, it must be possible for the
  33. >mechanism to cater for that.
  34.  
  35. All of this (and more) has been done, already.  I posted the "prf" package
  36. to "alt.sources" in December 1993.  An extract from the README is below.
  37.  
  38. >And, of course, you want a input conversion as well :-)
  39.  
  40. True, but that is missing in "prf", too.  :-(
  41.  
  42. >So instead of:
  43. >
  44. >> >         int bangcvt(va_list *ap, char *s, int len, fmtspec *f) {
  45. >
  46. >You'd get something more like:
  47. >
  48. >    bangcvt (va_list *ap, int flags, int width, int precision,
  49. >    char fmt, int (*putit)(char c, void *output), void *output)
  50. >
  51. >(Where you call "putit" for each character of output with *output
  52. >as magic for the call back)
  53.  
  54. In "prf" there are two structs, one to describe the conversion specification
  55. (from parsing the format string), and the other to describe the destination
  56. of the output.  To call a function for every single character is just
  57. one of the alternatives.  Others destinations are a simple FILE* or a limited
  58. memory buffer.  The above now looks like:
  59.  
  60.     bangcvt (PrfDest* dp, PrfSpec* sp, va_list* ap)
  61.  
  62. The following is an extract of the README for the "prf" package:
  63.  
  64. |This is the first public release of the ``prf'' library, an extended
  65. |and user programmable printf clone.
  66. |
  67. |For a description of function and interface see the manual page (prf.3x),
  68. |e.g. with "nroff -man prf.3x | col | more -s".  A manual page output
  69. |stripped down to plain ASCII can be found in "prf.3x.raw".
  70. |
  71. |In very short terms: what is this library good for?
  72. |- After initializing the prf library with e.g. "stdprf();fltprf();extprf();",
  73. |  you can call prf() instead of printf(), sprf() instead of sprintf,
  74. |  and fprf() instead of fprintf(), and should get the same output.
  75. |- You can use more/better conversions (e.g. prf("%b", 5) yields "101").
  76. |- You can write new conversion functions and bind them to, say, "%z".
  77. |- You can use other interface functions to have the output (format string
  78. |  with embedded conversions) go into whatever destination you want it.
  79. |
  80. |The intent of the copyright notice in the sources and the file COPYRIGHT
  81. |is to avoid legal problems and prevent others from claiming to be the author.
  82. |No further restrictions are intended.
  83.     [snip]
  84. |At the time the prf library were written, I had no ANSI C available.
  85. |Hence, it is all plain K&R C.  [snip]
  86. |
  87. |As far as possible and meaningful the prf library is consistent
  88. |with ``printf'' as described in SVID and BSD 4.3 printf(1) manual page.
  89. |As those descriptions are not at all complete, different behaviour
  90. |of printf and prf in some details should be expected.
  91.     [rest omitted]
  92.  
  93. The major drawback of the package seems to be that it is old K&R.
  94. It definitely should be redone in ANSI-C.  There are also some
  95. features still missing (e.g. flushing an arbitrary destination, or
  96. installing format call back functions just by loading).
  97.  
  98. All in all, I have found "prf" inspiring and quite useful for years.
  99. --
  100. Heiner Marxen            heiner@drb.insel.de
  101.